package com.resou.reader.reader.view;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.resou.reader.R;
import com.resou.reader.utils.UIHelper;
import com.resou.reader.view.recyclerviewfastscroll.viewprovider.DefaultBubbleBehavior;
import com.resou.reader.view.recyclerviewfastscroll.viewprovider.ScrollerViewProvider;
import com.resou.reader.view.recyclerviewfastscroll.viewprovider.ViewBehavior;
import com.resou.reader.view.recyclerviewfastscroll.viewprovider.VisibilityAnimationManager;

/**
 * 目录：右侧快速滑动
 *
 * @author UQIANTU.
 * @date 17/7/21.
 */

public class CatalogScrollerViewProvider extends ScrollerViewProvider {

    private View bubble;
    private View handle;
    private Context context;

    public CatalogScrollerViewProvider(Context context) {
        this.context = context;
    }

    @Override
    public View provideHandleView(ViewGroup container) {
        handle = new View(getContext());

//        int verticalInset = getContext().getResources().getDimensionPixelSize(R.dimen.spacing_dp_40);
//        int horizontalInset =  getContext().getResources().getDimensionPixelSize(R.dimen.spacing_dp_22);
//        InsetDrawable handleBg = new InsetDrawable(ContextCompat.getDrawable(getContext(), R.mipmap.ht_icon), horizontalInset, verticalInset, horizontalInset, verticalInset);
//        Utils.setBackground(handle, handleBg);
        handle.setBackgroundResource(R.mipmap.scoller_icon);

//        int handleWidth =context.getResources().getDimensionPixelSize(getScroller().isVertical() ? 22 : 40);
//        int handleHeight = context.getResources().getDimensionPixelSize(getScroller().isVertical() ?40 : 22);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(UIHelper.dip2px(getContext(),25 ), UIHelper.dip2px(getContext(),42 ));
//        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(handleWidth,handleHeight);
        params.setMargins(0, 0, 30, 0);// 设置间距
        handle.setLayoutParams(params);

        return handle;
    }

    @Override
    public View provideBubbleView(ViewGroup container) {
        bubble = LayoutInflater.from(getContext()).inflate(R.layout.fastscroll__default_bubble, container, false);
        return bubble;
    }

    @Override
    public TextView provideBubbleTextView() {
        return (TextView) bubble;
    }

    public int getBubbleOffset() {
        return (int) (getScroller().isVertical() ? ((float) handle.getHeight() / 2f) - bubble.getHeight() : ((float) handle.getWidth() / 2f) - bubble.getWidth());
    }

    @Override
    protected ViewBehavior provideHandleBehavior() {
        return null;
    }

    @Override
    protected ViewBehavior provideBubbleBehavior() {
        return new DefaultBubbleBehavior(new VisibilityAnimationManager.Builder(bubble).withPivotX(1f).withPivotY(1f).build());
    }
}
